package com.lzw.bluetoothpractice;

import android.app.Application;
import android.content.Context;
import android.os.Looper;
import android.util.Log;
import android.widget.Toast;

import com.airsaid.pickerviewlibrary.OptionsPickerView;
import com.alibaba.fastjson.JSONObject;
import com.king.thread.nevercrash.NeverCrash;
import com.lzw.bluetoothpractice.Models.Kneel;
import com.lzw.bluetoothpractice.Models.Temperature;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.CrashHandler;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import androidx.multidex.MultiDexApplication;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class MyApplication extends MultiDexApplication implements NeverCrash.CrashHandler {
    public static Context context;
    private Map<String, String> keyMap;
    public String step="0";
    public String angle="0";
    public String temperature="0";
    private static MyApplication instance;
    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        instance = this;
        context = getApplicationContext();

        super.onCreate();
    }

    public static MyApplication getInstance() {
        return instance;
    }
    public static Context getContext() {
        return context;
    }

    public void initInsustry(){




        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/message/messageId");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String userid = SharePreferenceUtils.getString(getContext(),"userId",null);
        String deviceId = SharePreferenceUtils.getString(getContext(),"DeviceId",null);;

        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;
        if(Double.parseDouble(temperature)<38||userid.isEmpty()||deviceId.isEmpty()){
            return;
        }
        Temperature temperatures=new Temperature();
        temperatures.setUserId(Long.parseLong(userid));
        temperatures.setDeviceId(Long.parseLong(deviceId));
        temperatures.setTemperature(Double.parseDouble(MyApplication.getInstance().temperature));
        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).addTemp(temperatures,"*:*:*","1", AppUtil.getVersionCode() + "", accessToken, keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("增加屈膝度记录",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {



                if(json.getInteger("code")==200){

                   // Toast toast=Toast.makeText(getContext(),"记录数据成功",Toast.LENGTH_LONG);
                  //  toast.show();


                }else {
                    //Toast toast=Toast.makeText(getContext(),"记录数据失败",Toast.LENGTH_LONG);
                    //toast.show();
                }


            }
        });
    }

    @Override
    public void uncaughtException(Thread t, Throwable e) {

        // 收集设备参数信息
        CrashHandler.collectDeviceInfo(context);
        // 保存日志文件
        CrashHandler.saveCrashInfo2File(e);
        // 使用 Toast 来显示异常信息
        new Thread() {
            @Override
            public void run() {
                Looper.prepare();
                Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
                Looper.loop();
            }
        }.start();
    }
}
